﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Booking.WPServiceData;
using Booking.lib;

namespace Booking
{
    public partial class Page2 : PhoneApplicationPage
    {
       
        public Page2()
        {
            InitializeComponent();
        }

        public void SetStatePage(string key, object value)
        {
            
            State.Add(key, value);
        }
        public Object GetStatePage(string key)
        {
            return State.ContainsKey(key)?State[key]:new Dictionary<int,int>();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            int id = 0;
            id = int.Parse(NavigationContext.QueryString["id"].ToString());
            int temp = DefinesClass.OrderState.ContainsKey(id) ? DefinesClass.OrderState[id] : 0;
            if (temp > 0)
            {
                DefinesClass.OrderState[id] = ++temp;
            }
            else
            {
                DefinesClass.OrderState.Add(id, 1);
            }
            int[] ids = DefinesClass.OrderState.Keys.ToArray();
            ServiceSoapClient service = new ServiceSoapClient();
            service.GetMenusByListIdCompleted += new EventHandler<GetMenusByListIdCompletedEventArgs>(fillData);
            service.GetMenusByListIdAsync(ids);
            base.OnNavigatedTo(e);
        }
        public void fillData(object sender, GetMenusByListIdCompletedEventArgs e)
        {
            List<lib.OrderMenus> OrderList = new List<OrderMenus>();
            foreach (menu _menu in e.Result)
            {
                OrderList.Add(new lib.OrderMenus() { Id = _menu.id, tenMon = _menu.name, SoLuong = DefinesClass.OrderState[_menu.id], Gia =  DefinesClass.OrderState[_menu.id] * _menu.price  });
            }
            lblLisMonAn.ItemsSource = OrderList;
            decimal tongcong = 0;
            foreach (OrderMenus _order in OrderList)
            {
                tongcong += _order.Gia;
            }
            gia.Text = "Tổng cộng: " + tongcong.ToString() + " VNĐ";
        }
        

        private void lblLisMonAn_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btndatmon_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Thongtin.xaml",UriKind.RelativeOrAbsolute));
        }


    }
}